package com.wrapp.example.floatlabelededittext.slice;

import com.wrapp.example.floatlabelededittext.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.TextField;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.PopupDialog;
import ohos.agp.window.service.WindowManager;
import ohos.multimodalinput.event.KeyEvent;
import ohos.multimodalinput.event.TouchEvent;

public class MainAbilitySlice extends AbilitySlice implements Component.DoubleClickedListener {
    private TextField textField1;
    private TextField textField2;
    private TextField textField3;
    private TextField textField4;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.getIntColor("#757575"));

        Component component = findComponentById(ResourceTable.Id_more);

        ShapeElement grayElement = new ShapeElement();
        grayElement.setRgbColor(new RgbColor(220, 220, 220));

        ShapeElement whiteElement = new ShapeElement();
        whiteElement.setRgbColor(new RgbColor(250, 250, 250));

        textField1 = (TextField) findComponentById(ResourceTable.Id_text1);
        textField2 = (TextField) findComponentById(ResourceTable.Id_text2);
        textField3 = (TextField) findComponentById(ResourceTable.Id_text3);
        textField4 = (TextField) findComponentById(ResourceTable.Id_text4);

        textField1.setDoubleClickedListener(this::onDoubleClick);
        textField2.setDoubleClickedListener(this::onDoubleClick);
        textField3.setDoubleClickedListener(this::onDoubleClick);
        textField4.setDoubleClickedListener(this::onDoubleClick);

        // 设置光标颜色
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setShape(ShapeElement.LINE);
        RgbColor rgbColor = new RgbColor(0, 154, 134);
        shapeElement.setRgbColors(new RgbColor[]{rgbColor, rgbColor});
        shapeElement.setStroke(AttrHelper.vp2px(28, this), rgbColor);
        textField1.setCursorElement(shapeElement);
        textField2.setCursorElement(shapeElement);
        textField3.setCursorElement(shapeElement);
        textField4.setCursorElement(shapeElement);

        textField3.setKeyEventListener(new Component.KeyEventListener() {
            @Override
            public boolean onKeyEvent(Component component, KeyEvent keyEvent) {
                if (keyEvent.getKeyCode() != KeyEvent.KEY_ENTER) {
                    return false;
                }
                textField3.setText(textField3.getText().replaceAll("\n", ""));
                return false;
            }
        });

        textField4.setKeyEventListener(new Component.KeyEventListener() {
            @Override
            public boolean onKeyEvent(Component component, KeyEvent keyEvent) {
                if (keyEvent.getKeyCode() == KeyEvent.KEY_ENTER) {
                    textField4.setText(textField4.getText().replaceAll("\n", ""));
                }
                return false;
            }
        });

        component.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                PopupDialog dialog = new PopupDialog(MainAbilitySlice.this, null);
                Component parse = LayoutScatter.getInstance(MainAbilitySlice.this).parse(ResourceTable.Layout_dialog_layout, null, false);
                dialog.setCustomComponent(parse);
                dialog.setAutoClosable(true);
                dialog.showOnCertainPosition(LayoutAlignment.RIGHT | LayoutAlignment.TOP, 20, 100);
                dialog.show();
                parse.setTouchEventListener(new Component.TouchEventListener() {
                    @Override
                    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                        switch (touchEvent.getAction()) {
                            case TouchEvent.PRIMARY_POINT_DOWN:
                                parse.setBackground(grayElement);
                                break;
                            case TouchEvent.PRIMARY_POINT_UP:
                                parse.setBackground(whiteElement);
                                break;
                        }
                        return false;
                    }
                });
                Component settings = parse.findComponentById(ResourceTable.Id_btn);
                settings.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        dialog.remove();
                    }
                });
            }
        });
    }

    @Override
    public void onDoubleClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_text1:
                textField1.executeLongClick();
                break;
            case ResourceTable.Id_text2:
                textField2.executeLongClick();
                break;
            case ResourceTable.Id_text3:
                textField3.executeLongClick();
                break;
            case ResourceTable.Id_text4:
                textField4.executeLongClick();
                break;
        }
    }
}